from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *

from module_constants import *

####################################################################################################################
#  Each trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Delay interval: Time to wait before applying the consequences of the trigger
#    After its conditions have been evaluated as true.
# 3) Re-arm interval. How much time must pass after applying the consequences of the trigger for the trigger to become active again.
#    You can put the constant ti_once here to make sure that the trigger never becomes active again after it fires once.
# 4) Conditions block (list). This must be a valid operation block. See header_operations.py for reference.
#    Every time the trigger is checked, the conditions block will be executed.
#    If the conditions block returns true, the consequences block will be executed.
#    If the conditions block is empty, it is assumed that it always evaluates to true.
# 5) Consequences block (list). This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################

# Some constants for use below
merchant_inventory_space = 30
num_merchandise_goods = 36

triggers = [

(0.100000, 0.000000, ti_once,
[
    (map_free, 0),
],
[
    (dialog_box, "str_tutorial_map1"),
]),

(0.000000, 0.000000, 24.000000,
[
],
[
    (reset_item_probabilities, 100),
    (set_merchandise_modifier_quality, 100),
    (reset_item_probabilities, 100),
    (set_item_probability_in_merchandise, "itm_salt", 700),
    (troop_add_merchandise, "trp_salt_mine_merchant", 11, 36),
    (try_begin),
        (try_for_range, ":var_0", 0, 3),
            (store_random_in_range, ":var_1", "itm_smoked_fish", "itm_baggage_train"),
            (troop_add_item, "trp_order_quartermaster", ":var_1", 0),
        (try_end),
        (troop_ensure_inventory_space, "trp_order_quartermaster", 6),
        (troop_sort_inventory, "trp_order_quartermaster"),
    (try_end),
    (store_sub, ":var_2", 301, "itm_spice"),
    (try_for_range, ":var_3", "p_town_1", "p_castle_1"),
        (party_get_slot, ":var_4", ":var_3", slot_town_merchant),
        (reset_item_probabilities, 100),
        (try_for_range, ":var_5", "itm_spice", "itm_baggage_train"),
            (store_add, ":var_6", ":var_5", ":var_2"),
            (party_get_slot, ":var_7", ":var_3", ":var_6"),
            (call_script, "script_center_get_production", ":var_3", ":var_5"),
            (assign, ":var_8", reg0),
            (call_script, "script_center_get_consumption", ":var_3", ":var_5"),
            (val_sub, ":var_8", reg0),
            (val_abs, ":var_8"),
            (val_min, ":var_8", 50),
            (val_mul, ":var_8", 6),
            (val_mul, ":var_8", 1000),
            (val_div, ":var_8", ":var_7"),
            (val_mul, ":var_8", 1000),
            (val_div, ":var_8", ":var_7"),
            (val_mul, ":var_8", 1000),
            (val_div, ":var_8", ":var_7"),
            (set_item_probability_in_merchandise, ":var_5", ":var_8"),
        (try_end),
        (troop_add_merchandise, ":var_4", 11, 36),
        (troop_ensure_inventory_space, ":var_4", 55),
        (troop_sort_inventory, ":var_4"),
        (store_troop_gold, ":var_9", ":var_4"),
        (neg|ge, ":var_9", 10000),
        (store_random_in_range, ":var_10", 5000, 8000),
        (call_script, "script_troop_add_gold", ":var_4", ":var_10"),
    (try_end),
]),

(0.000000, 0.000000, 168.000000,
[
],
[
    (reset_item_probabilities, 100),
    (set_merchandise_modifier_quality, 120),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_warrior", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_warrior", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_ranger", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_ranger", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_noble", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_maiden", 300),
    (troop_loot_troop, "trp_noldor_merchant", "trp_noldor_knight", 300),
    (troop_get_inventory_capacity, ":var_0", "trp_noldor_merchant"),
    (try_for_range, ":var_1", 0, ":var_0"),
        (troop_get_inventory_slot, ":var_2", "trp_noldor_merchant", ":var_1"),
        (ge, ":var_2", 0),
        (try_begin),
            (this_or_next|eq, ":var_2", "itm_pop_loot"),
            (eq, ":var_2", "itm_pop_noldor_goods"),
            (troop_set_inventory_slot, "trp_noldor_merchant", ":var_1", -1),
        (try_end),
    (try_end),
    (troop_ensure_inventory_space, "trp_noldor_merchant", 30),
    (troop_sort_inventory, "trp_noldor_merchant"),
    (store_troop_gold, reg6, "trp_noldor_merchant"),
    (try_begin),
        (neg|ge, reg6, 900),
        (store_random_in_range, ":var_3", 2800, 5000),
        (call_script, "script_troop_add_gold", "trp_noldor_merchant", ":var_3"),
    (try_end),
    (try_for_range, ":var_4", "trp_town_1_armorer", "trp_town_1_weaponsmith"),
        (store_sub, ":var_5", ":var_4", "trp_town_1_armorer"),
        (val_add, ":var_5", "p_town_1"),
        (troop_clear_inventory, ":var_4"),
        (party_get_slot, ":var_6", ":var_5", slot_center_original_faction),
        (troop_add_merchandise_with_faction, ":var_4", ":var_6", 13, 16),
        (troop_add_merchandise_with_faction, ":var_4", ":var_6", 12, 16),
        (troop_add_merchandise_with_faction, ":var_4", ":var_6", 14, 8),
        (troop_add_merchandise_with_faction, ":var_4", ":var_6", 15, 4),
        (troop_ensure_inventory_space, ":var_4", 40),
        (troop_sort_inventory, ":var_4"),
        (store_troop_gold, reg6, ":var_4"),
        (neg|ge, reg6, 10000),
        (store_random_in_range, ":var_3", 8000, 14000),
        (call_script, "script_troop_add_gold", ":var_4", ":var_3"),
    (try_end, 0),
]),

(0.000000, 0.000000, 168.000000,
[
],
[
    (reset_item_probabilities, 100),
    (set_merchandise_modifier_quality, 120),
    (try_for_range, ":var_0", "trp_town_1_weaponsmith", "trp_town_1_tavernkeeper"),
        (store_sub, ":var_1", ":var_0", "trp_town_1_weaponsmith"),
        (val_add, ":var_1", "p_town_1"),
        (troop_clear_inventory, ":var_0"),
        (party_get_slot, ":var_2", ":var_1", slot_center_original_faction),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 2, 9),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 3, 4),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 4, 4),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 7, 8),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 8, 5),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 9, 3),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 10, 2),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 5, 3),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 6, 2),
        (troop_ensure_inventory_space, ":var_0", 40),
        (troop_sort_inventory, ":var_0"),
        (store_troop_gold, reg6, ":var_0"),
        (neg|ge, reg6, 10000),
        (store_random_in_range, ":var_3", 8000, 14000),
        (call_script, "script_troop_add_gold", ":var_0", ":var_3"),
    (try_end, 0),
]),

(0.000000, 0.000000, 168.000000,
[
],
[
    (reset_item_probabilities, 100),
    (set_merchandise_modifier_quality, 120),
    (try_for_range, ":var_0", "trp_town_1_horse_merchant", "trp_town_1_mayor"),
        (troop_clear_inventory, ":var_0"),
        (store_sub, ":var_1", ":var_0", "trp_town_1_horse_merchant"),
        (val_add, ":var_1", "p_town_1"),
        (party_get_slot, ":var_2", ":var_1", slot_center_original_faction),
        (store_random_in_range, ":var_3", 5, 16),
        (troop_add_merchandise_with_faction, ":var_0", ":var_2", 1, ":var_3"),
        (troop_ensure_inventory_space, ":var_0", 65),
        (troop_sort_inventory, ":var_0"),
        (store_troop_gold, ":var_4", ":var_0"),
        (neg|ge, ":var_4", 10000),
        (store_random_in_range, ":var_5", 8000, 15000),
        (call_script, "script_troop_add_gold", ":var_0", ":var_5"),
    (try_end),
]),

(1.000000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_track_down_bandits"),
    (neg|check_quest_failed, "qst_track_down_bandits"),
    (neg|check_quest_succeeded, "qst_track_down_bandits"),
],
[
    (quest_get_slot, ":var_0", "qst_track_down_bandits", 8),
    (try_begin),
        (party_is_active, ":var_0"),
        (store_faction_of_party, ":var_1", ":var_0"),
        (neg|is_between, ":var_1", "fac_player_supporters_faction", "fac_kingdoms_end"),
        (assign, ":var_2", 8),
        (try_begin),
            (is_currently_night),
            (assign, ":var_2", 5),
        (try_end),
        (try_for_parties, ":var_3"),
            (gt, ":var_3", "p_spawn_points_end"),
            (store_faction_of_party, ":var_4", ":var_3"),
            (is_between, ":var_4", "fac_player_supporters_faction", "fac_kingdoms_end"),
            (store_distance_to_party_from_party, ":var_5", ":var_3", ":var_0"),
            (neg|ge, ":var_5", ":var_2"),
            (try_begin),
                (eq, "$cheat_mode", 1),
                (str_store_party_name, s4, ":var_3"),
                (display_message, "@{!}DEBUG -- Wanted bandits spotted by {s4}"),
            (try_end),
            (call_script, "script_get_closest_center", ":var_0"),
            (assign, ":var_6", reg0),
            (call_script, "script_add_log_entry", 70, ":var_3", ":var_6", ":var_0", -1),
        (try_end),
    (else_try),
        (display_message, "str_bandits_eliminated_by_another", 0x66CCFF),
        (call_script, "script_abort_quest", "qst_track_down_bandits", 0),
    (try_end),
]),

(2.000000, 0.000000, 0.000000,
[
    (store_random_party_of_template, reg2, "pt_prisoner_train_party"),
    (party_is_in_any_town, reg2),
],
[
    (store_faction_of_party, ":var_0", reg2),
    (call_script, "script_cf_select_random_walled_center_with_faction", ":var_0", -1),
    (party_set_ai_behavior, reg2, 1),
    (party_set_ai_object, reg2, reg0),
    (party_set_flags, reg2, 65536, 0),
]),

(4.000000, 0.000000, 0.000000,
[
    (eq, "$caravan_escort_state", 1),
    (assign, ":var_0", 0),
    (try_begin),
        (neg|party_is_active, "$caravan_escort_party_id"),
        (assign, ":var_0", 1),
    (else_try),
        (get_party_ai_object, ":var_1", "$caravan_escort_party_id"),
        (neg|eq, ":var_1", "$caravan_escort_destination_town"),
        (assign, ":var_0", 1),
    (try_end),
    (eq, ":var_0", 1),
],
[
    (assign, "$caravan_escort_state", 0),
]),

(1.500000, 0.000000, 0.000000,
[
    (store_random_party_of_template, reg2, "pt_messenger_party"),
    (party_is_in_any_town, reg2),
],
[
    (store_faction_of_party, ":var_0", reg2),
    (call_script, "script_cf_select_random_walled_center_with_faction", ":var_0", -1),
    (party_set_ai_behavior, reg2, 1),
    (party_set_ai_object, reg2, reg0),
    (party_set_flags, reg2, 65536, 0),
]),

(1.000000, 0.000000, 0.000000,
[
],
[
    (try_for_range, ":var_0", "fac_player_supporters_faction", "fac_kingdoms_end"),
        (faction_slot_eq, ":var_0", slot_faction_state, 0),
        (try_begin),
            (store_random_in_range, ":var_1", 0, 100),
            (neg|ge, ":var_1", 10),
            (call_script, "script_create_kingdom_party_if_below_limit", ":var_0", 11),
        (try_end),
    (try_end),
]),

(0.200000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_incriminate_loyal_commander"),
    (neg|check_quest_concluded, "qst_incriminate_loyal_commander"),
    (quest_slot_eq, "qst_incriminate_loyal_commander", 11, 2),
    (quest_get_slot, ":var_0", "qst_incriminate_loyal_commander", 1),
    (quest_get_slot, ":var_1", "qst_incriminate_loyal_commander", 8),
    (try_begin),
        (neg|party_is_active, ":var_1"),
        (quest_set_slot, "qst_incriminate_loyal_commander", 11, 3),
        (call_script, "script_fail_quest", "qst_incriminate_loyal_commander"),
    (else_try),
        (party_is_in_town, ":var_1", ":var_0"),
        (remove_party, ":var_1"),
        (quest_set_slot, "qst_incriminate_loyal_commander", 11, 3),
        (quest_get_slot, ":var_2", "qst_incriminate_loyal_commander", 4),
        (assign, ":var_3", 0),
        (try_for_range, ":var_4", "fac_player_supporters_faction", "fac_kingdoms_end"),
            (faction_slot_eq, ":var_4", slot_faction_state, 0),
            (neg|eq, ":var_4", "fac_player_supporters_faction"),
            (neg|quest_slot_eq, "qst_incriminate_loyal_commander", 3, ":var_4"),
            (val_add, ":var_3", 1),
        (try_end),
        (try_begin),
            (gt, ":var_3", 0),
            (store_random_in_range, ":var_5", 0, ":var_3"),
            (assign, ":var_6", -1),
            (try_for_range, ":var_4", "fac_player_supporters_faction", "fac_kingdoms_end"),
                (eq, ":var_6", -1),
                (faction_slot_eq, ":var_4", slot_faction_state, 0),
                (neg|eq, ":var_4", "fac_player_supporters_faction"),
                (neg|quest_slot_eq, "qst_incriminate_loyal_commander", 3, ":var_4"),
                (val_sub, ":var_5", 1),
                (neg|ge, ":var_5", 0),
                (assign, ":var_6", ":var_4"),
            (try_end),
        (try_end),
        (try_begin),
            (gt, ":var_6", 0),
            (call_script, "script_change_troop_faction", ":var_2", ":var_6"),
        (else_try),
            (call_script, "script_change_troop_faction", ":var_2", "fac_robber_knights"),
        (try_end),
        (call_script, "script_succeed_quest", "qst_incriminate_loyal_commander"),
    (try_end),
],
[
]),

(0.200000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_bring_back_runaway_serfs"),
    (neg|check_quest_concluded, "qst_bring_back_runaway_serfs"),
    (quest_get_slot, ":var_0", "qst_bring_back_runaway_serfs", 7),
    (quest_get_slot, ":var_1", "qst_bring_back_runaway_serfs", 1),
    (try_begin),
        (party_is_active, "$qst_bring_back_runaway_serfs_party_1"),
        (try_begin),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":var_1"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
        (else_try),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":var_0"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
        (else_try),
            (store_distance_to_party_from_party, ":var_2", "p_main_party", "$qst_bring_back_runaway_serfs_party_1"),
            (gt, ":var_2", 3),
            (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_1", ":var_1"),
        (try_end),
    (try_end),
    (try_begin),
        (party_is_active, "$qst_bring_back_runaway_serfs_party_2"),
        (try_begin),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":var_1"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
        (else_try),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":var_0"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
        (else_try),
            (store_distance_to_party_from_party, ":var_2", "p_main_party", "$qst_bring_back_runaway_serfs_party_2"),
            (gt, ":var_2", 3),
            (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_2", ":var_1"),
        (try_end),
    (try_end),
    (try_begin),
        (party_is_active, "$qst_bring_back_runaway_serfs_party_3"),
        (try_begin),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":var_1"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
        (else_try),
            (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":var_0"),
            (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
            (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
        (else_try),
            (store_distance_to_party_from_party, ":var_2", "p_main_party", "$qst_bring_back_runaway_serfs_party_3"),
            (gt, ":var_2", 3),
            (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_3", ":var_1"),
        (try_end),
    (try_end),
    (assign, ":var_3", "$qst_bring_back_runaway_serfs_num_parties_returned"),
    (val_add, ":var_3", "$qst_bring_back_runaway_serfs_num_parties_fleed"),
    (ge, ":var_3", 3),
    (try_begin),
        (ge, "$qst_bring_back_runaway_serfs_num_parties_returned", 3),
        (call_script, "script_succeed_quest", "qst_bring_back_runaway_serfs"),
    (else_try),
        (eq, "$qst_bring_back_runaway_serfs_num_parties_returned", 0),
        (call_script, "script_fail_quest", "qst_bring_back_runaway_serfs"),
    (else_try),
        (call_script, "script_conclude_quest", "qst_bring_back_runaway_serfs"),
    (try_end),
],
[
]),

(0.500000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_follow_spy"),
    (eq, "$qst_follow_spy_no_active_parties", 0),
    (quest_get_slot, ":var_0", "qst_follow_spy", 12),
    (quest_get_slot, ":var_1", "qst_follow_spy", 7),
    (assign, ":var_2", 0),
    (try_begin),
        (this_or_next|ge, "$qst_follow_spy_run_away", 2),
        (this_or_next|neg|party_is_active, "$qst_follow_spy_spy_party"),
        (neg|party_is_active, "$qst_follow_spy_spy_partners_party"),
    (else_try),
        (eq, "$qst_follow_spy_meeting_state", 0),
        (store_distance_to_party_from_party, ":var_3", "p_main_party", "$qst_follow_spy_spy_party"),
        (try_begin),
            (assign, ":var_4", 3),
            (try_begin),
                (is_currently_night),
                (assign, ":var_4", 1),
            (try_end),
            (neg|gt, ":var_3", ":var_4"),
            (store_distance_to_party_from_party, ":var_5", "p_main_party", ":var_0"),
            (gt, ":var_5", 1),
            (val_add, "$qst_follow_spy_run_away", 1),
            (try_begin),
                (eq, "$qst_follow_spy_run_away", 2),
                (assign, ":var_2", 1),
                (display_message, "str_qst_follow_spy_noticed_you"),
            (try_end),
        (else_try),
            (store_distance_to_party_from_party, ":var_3", "$qst_follow_spy_spy_partners_party", "$qst_follow_spy_spy_party"),
            (neg|gt, ":var_3", 1),
            (party_attach_to_party, "$qst_follow_spy_spy_party", "$qst_follow_spy_spy_partners_party"),
            (assign, "$qst_follow_spy_meeting_state", 1),
            (assign, "$qst_follow_spy_meeting_counter", 0),
        (try_end),
    (else_try),
        (eq, "$qst_follow_spy_meeting_state", 1),
        (store_distance_to_party_from_party, ":var_3", "p_main_party", "$qst_follow_spy_spy_partners_party"),
        (try_begin),
            (neg|gt, ":var_3", 1),
            (party_detach, "$qst_follow_spy_spy_party"),
            (val_add, "$qst_follow_spy_run_away", 1),
            (try_begin),
                (eq, "$qst_follow_spy_run_away", 2),
                (assign, ":var_2", 1),
                (display_message, "str_qst_follow_spy_noticed_you"),
            (try_end),
        (else_try),
            (val_add, "$qst_follow_spy_meeting_counter", 1),
            (gt, "$qst_follow_spy_meeting_counter", 4),
            (party_detach, "$qst_follow_spy_spy_party"),
            (assign, ":var_2", 1),
            (assign, "$qst_follow_spy_meeting_state", 2),
        (try_end),
    (try_end),
    (try_begin),
        (eq, ":var_2", 1),
        (party_set_ai_object, "$qst_follow_spy_spy_party", ":var_0"),
        (party_set_ai_object, "$qst_follow_spy_spy_partners_party", ":var_1"),
        (party_set_ai_behavior, "$qst_follow_spy_spy_party", 1),
        (party_set_ai_behavior, "$qst_follow_spy_spy_partners_party", 1),
        (party_set_flags, "$qst_follow_spy_spy_party", 65536, 0),
        (party_set_flags, "$qst_follow_spy_spy_partners_party", 65536, 0),
    (try_end),
    (assign, ":var_6", 0),
    (try_begin),
        (party_is_active, "$qst_follow_spy_spy_party"),
        (val_add, ":var_6", 1),
        (party_is_in_town, "$qst_follow_spy_spy_party", ":var_0"),
        (remove_party, "$qst_follow_spy_spy_party"),
        (assign, "$qst_follow_spy_spy_back_in_town", 1),
        (val_sub, ":var_6", 1),
    (try_end),
    (try_begin),
        (party_is_active, "$qst_follow_spy_spy_partners_party"),
        (val_add, ":var_6", 1),
        (party_is_in_town, "$qst_follow_spy_spy_partners_party", ":var_1"),
        (remove_party, "$qst_follow_spy_spy_partners_party"),
        (assign, "$qst_follow_spy_partner_back_in_town", 1),
        (val_sub, ":var_6", 1),
    (try_end),
    (try_begin),
        (eq, "$qst_follow_spy_partner_back_in_town", 1),
        (eq, "$qst_follow_spy_spy_back_in_town", 1),
        (call_script, "script_fail_quest", "qst_follow_spy"),
    (try_end),
    (try_begin),
        (eq, ":var_6", 0),
        (assign, "$qst_follow_spy_no_active_parties", 1),
        (party_count_prisoners_of_type, ":var_7", "p_main_party", "trp_spy"),
        (party_count_prisoners_of_type, ":var_8", "p_main_party", "trp_spy_partner"),
        (gt, ":var_7", 0),
        (gt, ":var_8", 0),
        (call_script, "script_succeed_quest", "qst_follow_spy"),
    (try_end),
],
[
]),

(168.000000, 0.000000, 0.000000,
[
],
[
    (val_mul, "$debt_to_merchants_guild", 101),
    (val_div, "$debt_to_merchants_guild", 100),
]),

(0.100000, 0.000000, 0.100000,
[
    (check_quest_active, "qst_escort_merchant_caravan"),
    (eq, "$escort_merchant_caravan_mode", 1),
],
[
    (quest_get_slot, ":var_0", "qst_escort_merchant_caravan", 8),
    (try_begin),
        (party_is_active, ":var_0"),
        (party_set_ai_behavior, ":var_0", 0),
        (party_set_flags, ":var_0", 65536, 0),
    (try_end),
]),

(0.100000, 0.000000, 0.100000,
[
    (check_quest_active, "qst_escort_merchant_caravan"),
    (eq, "$escort_merchant_caravan_mode", 0),
],
[
    (quest_get_slot, ":var_0", "qst_escort_merchant_caravan", 8),
    (try_begin),
        (party_is_active, ":var_0"),
        (party_set_ai_behavior, ":var_0", 10),
        (party_set_flags, ":var_0", 65536, 0),
        (party_set_ai_object, ":var_0", "p_main_party"),
    (try_end),
]),

(0.100000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_escort_merchant_caravan"),
    (quest_get_slot, ":var_0", "qst_escort_merchant_caravan", 8),
    (neg|party_is_active, ":var_0"),
],
[
    (call_script, "script_abort_quest", "qst_escort_merchant_caravan", 2),
]),

(0.100000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_escort_peasants"),
    (neg|check_quest_succeeded, "qst_escort_peasants"),
    (quest_get_slot, ":var_0", "qst_escort_peasants", 8),
    (neg|party_is_active, ":var_0"),
],
[
    (call_script, "script_abort_quest", "qst_escort_peasants", 2),
]),

(0.100000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_escort_peasants"),
    (neg|check_quest_succeeded, "qst_escort_peasants"),
    (quest_get_slot, ":var_0", "qst_escort_peasants", 8),
    (party_is_active, ":var_0"),
    (party_is_in_any_town, ":var_0"),
    (party_get_cur_town, ":var_1", ":var_0"),
    (quest_slot_eq, "qst_escort_peasants", 1, ":var_1"),
    (remove_party, ":var_0"),
],
[
    (call_script, "script_succeed_quest", "qst_escort_peasants"),
]),

(0.300000, 0.000000, 1.100000,
[
    (check_quest_active, "qst_troublesome_bandits"),
    (neg|check_quest_failed, "qst_troublesome_bandits"),
    (store_num_parties_destroyed, ":var_0", "pt_troublesome_bandits"),
    (neg|ge, "$qst_troublesome_bandits_eliminated", ":var_0"),
    (store_num_parties_destroyed_by_player, ":var_1", "pt_troublesome_bandits"),
    (eq, ":var_1", "$qst_troublesome_bandits_eliminated_by_player"),
],
[
    (display_message, "str_bandits_eliminated_by_another", 0x66CCFF),
    (call_script, "script_abort_quest", "qst_troublesome_bandits", 0),
]),

(0.300000, 0.000000, 1.100000,
[
    (check_quest_active, "qst_troublesome_bandits"),
    (neg|check_quest_succeeded, "qst_troublesome_bandits"),
    (store_num_parties_destroyed, ":var_0", "pt_troublesome_bandits"),
    (neg|ge, "$qst_troublesome_bandits_eliminated", ":var_0"),
    (store_num_parties_destroyed_by_player, ":var_1", "pt_troublesome_bandits"),
    (neg|eq, ":var_1", "$qst_troublesome_bandits_eliminated_by_player"),
],
[
    (call_script, "script_succeed_quest", "qst_troublesome_bandits"),
]),

(0.300000, 0.000000, 1.100000,
[
    (check_quest_active, "qst_marauding_jatu"),
    (neg|check_quest_succeeded, "qst_marauding_jatu"),
    (store_num_parties_destroyed, ":var_0", "pt_jatu_quest_army"),
    (gt, ":var_0", 0),
],
[
    (call_script, "script_succeed_quest", "qst_marauding_jatu"),
]),

(0.300000, 0.000000, 1.100000,
[
    (check_quest_active, "qst_order_rivalry"),
    (neg|check_quest_succeeded, "qst_order_rivalry"),
    (neg|check_quest_failed, "qst_order_rivalry"),
    (quest_get_slot, ":var_0", "qst_order_rivalry", 8),
    (neg|party_is_active, ":var_0"),
],
[
    (call_script, "script_succeed_quest", "qst_order_rivalry"),
]),

(1.000000, 0.000000, 0.000000,
[
    (check_quest_active, "qst_kidnapped_girl"),
    (quest_get_slot, ":var_0", "qst_kidnapped_girl", 8),
    (party_is_active, ":var_0"),
    (party_is_in_any_town, ":var_0"),
    (remove_party, ":var_0"),
],
[
]),

(0.000000, 0.000000, 336.000000,
[
    (try_for_range, ":var_0", "trp_kingdom_1_pretender", "trp_knight_1_1_wife"),
        (troop_set_slot, ":var_0", slot_troop_cur_center, 0),
        (neg|eq, ":var_0", "$supported_pretender"),
        (troop_get_slot, ":var_1", ":var_0", slot_troop_original_faction),
        (faction_slot_eq, ":var_1", slot_faction_state, 0),
        (faction_slot_eq, ":var_1", slot_faction_has_rebellion_chance, 1),
        (neg|troop_slot_eq, ":var_0", slot_troop_occupation, 2),
        (try_for_range, ":var_2", 0, 30),
            (troop_slot_eq, ":var_0", slot_troop_cur_center, 0),
            (store_random_in_range, ":var_3", "p_town_1", "p_castle_1"),
            (store_faction_of_party, ":var_4", ":var_3"),
            (store_relation, ":var_5", ":var_4", ":var_1"),
            (neg|gt, ":var_5", 0),
            (troop_set_slot, ":var_0", slot_troop_cur_center, ":var_3"),
            (try_begin),
                (eq, "$cheat_mode", 1),
                (str_store_troop_name, s4, ":var_0"),
                (str_store_party_name, s5, ":var_3"),
                (display_message, "@{!}{s4} is in {s5}"),
            (try_end),
        (try_end),
    (try_end),
],
[
]),

(360.000000, 0.000000, 0.000000,
[
    (call_script, "script_update_companion_candidates_in_taverns"),
],
[
]),

(0.000000, 0.000000, 24.000000,
[
],
[
    (assign, ":var_0", 0),
    (assign, ":var_1", 100),
    (try_for_range, ":var_2", "trp_npc_adonja", "trp_kingdom_1_lord"),
        (main_party_has_troop, ":var_2"),
        (val_add, ":var_0", 1),
    (try_end),
    (val_sub, ":var_1", ":var_0"),
    (store_skill_level, ":var_3", "skl_persuasion", "trp_player"),
    (val_add, ":var_1", ":var_3"),
    (assign, reg7, ":var_1"),
    (try_begin),
        (gt, "$personality_clash_after_24_hrs", 0),
        (try_begin),
            (troop_get_slot, ":var_4", "$personality_clash_after_24_hrs", slot_troop_personalityclash_object),
            (main_party_has_troop, "$personality_clash_after_24_hrs"),
            (main_party_has_troop, ":var_4"),
            (try_begin),
                (neg|eq, "$g_option_companion_dialogs", 0),
                (assign, "$npc_with_personality_clash", "$personality_clash_after_24_hrs"),
            (else_try),
                (troop_get_slot, ":var_5", "$personality_clash_after_24_hrs", slot_troop_personalityclash_penalties),
                (val_add, ":var_5", 5),
                (troop_set_slot, "$personality_clash_after_24_hrs", slot_troop_personalityclash_penalties, ":var_5"),
                (troop_set_slot, "$personality_clash_after_24_hrs", slot_troop_personalityclash_state, 3),
                (troop_get_slot, ":var_4", "$personality_clash_after_24_hrs", slot_troop_personalityclash_object),
                (str_store_troop_name, s11, "$personality_clash_after_24_hrs"),
                (str_store_troop_name, s12, ":var_4"),
                (display_message, "@{s11} and {s12} are upset at each other.", 0xFFFF00),
                (assign, "$npc_with_personality_clash", 0),
            (try_end),
        (try_end),
        (assign, "$personality_clash_after_24_hrs", 0),
    (try_end),
    (try_for_range, ":var_6", "trp_npc_adonja", "trp_kingdom_1_lord"),
        (troop_set_slot, ":var_6", slot_troop_turned_down_twice, 0),
        (try_begin),
            (troop_slot_eq, ":var_6", slot_troop_met, 1),
            (troop_set_slot, ":var_6", slot_troop_met_previously, 1),
        (try_end),
        (troop_get_slot, ":var_7", ":var_6", slot_troop_occupation),
        (try_begin),
            (eq, ":var_7", 11),
            (troop_get_slot, ":var_8", ":var_6", slot_troop_return_renown),
            (str_store_troop_name, s31, ":var_6"),
            (troop_get_slot, ":var_9", "trp_player", slot_troop_renown),
            (assign, reg4, ":var_9"),
            (assign, reg5, ":var_8"),
            (gt, ":var_9", ":var_8"),
            (troop_set_slot, ":var_6", slot_troop_personalityclash_penalties, 0),
            (troop_set_slot, ":var_6", slot_troop_morality_penalties, 0),
            (troop_set_slot, ":var_6", slot_troop_occupation, 0),
        (try_end),
        (try_begin),
            (troop_slot_ge, ":var_6", slot_troop_current_mission, 5),
            (troop_slot_eq, ":var_6", slot_troop_mission_object, 1),
            (troop_get_slot, ":var_4", ":var_6", slot_troop_kingsupport_opponent),
            (main_party_has_troop, ":var_4"),
            (troop_slot_eq, ":var_4", slot_troop_kingsupport_objection_state, 0),
            (troop_set_slot, ":var_4", slot_troop_kingsupport_objection_state, 1),
            (str_store_troop_name, s3, ":var_6"),
            (str_store_troop_name, s4, ":var_4"),
            (try_begin),
                (eq, "$cheat_mode", 1),
                (display_message, "str_s4_ready_to_voice_objection_to_s3s_mission_if_in_party"),
            (try_end),
        (try_end),
        (try_begin),
            (main_party_has_troop, ":var_6"),
            (call_script, "script_npc_morale", ":var_6"),
            (assign, ":var_10", reg0),
            (try_begin),
                (neg|ge, ":var_10", 20),
                (store_random_in_range, ":var_11", 0, 100),
                (val_add, ":var_10", ":var_11"),
                (neg|ge, ":var_10", 20),
                (assign, "$npc_is_quitting", ":var_6"),
            (try_end),
            (troop_get_slot, ":var_5", ":var_6", slot_troop_personalityclash_penalties),
            (val_mul, ":var_5", 90),
            (val_div, ":var_5", ":var_1"),
            (troop_set_slot, ":var_6", slot_troop_personalityclash_penalties, ":var_5"),
            (troop_get_slot, ":var_5", ":var_6", slot_troop_morality_penalties),
            (val_mul, ":var_5", 90),
            (val_div, ":var_5", ":var_1"),
            (troop_set_slot, ":var_6", slot_troop_morality_penalties, ":var_5"),
            (try_begin),
                (this_or_next|troop_slot_ge, ":var_6", slot_troop_personalityclash_state, 1),
                (eq, "$disable_npc_complaints", 1),
                (troop_get_slot, ":var_12", ":var_6", slot_troop_personalityclash_object),
                (main_party_has_troop, ":var_12"),
                (call_script, "script_reduce_companion_morale_for_clash", ":var_6", ":var_12", 72),
            (try_end),
            (try_begin),
                (this_or_next|troop_slot_ge, ":var_6", slot_troop_personalityclash2_state, 1),
                (eq, "$disable_npc_complaints", 1),
                (troop_get_slot, ":var_12", ":var_6", slot_troop_personalityclash2_object),
                (main_party_has_troop, ":var_12"),
                (call_script, "script_reduce_companion_morale_for_clash", ":var_6", ":var_12", 74),
            (try_end),
            (try_begin),
                (this_or_next|troop_slot_ge, ":var_6", slot_troop_personalitymatch_state, 1),
                (eq, "$disable_npc_complaints", 1),
                (troop_get_slot, ":var_12", ":var_6", slot_troop_personalitymatch_object),
                (main_party_has_troop, ":var_12"),
                (troop_get_slot, ":var_5", ":var_6", slot_troop_personalityclash_penalties),
                (val_mul, ":var_5", 9),
                (val_div, ":var_5", 10),
                (troop_set_slot, ":var_6", slot_troop_personalityclash_penalties, ":var_5"),
            (try_end),
            (try_begin),
                (eq, "$npc_with_personality_clash", 0),
                (eq, "$npc_with_personality_clash_2", 0),
                (eq, "$personality_clash_after_24_hrs", 0),
                (troop_slot_eq, ":var_6", slot_troop_personalityclash_state, 0),
                (troop_get_slot, ":var_4", ":var_6", slot_troop_personalityclash_object),
                (main_party_has_troop, ":var_4"),
                (assign, "$personality_clash_after_24_hrs", ":var_6"),
            (try_end),
            (try_begin),
                (eq, "$npc_with_political_grievance", 0),
                (troop_slot_eq, ":var_6", slot_troop_kingsupport_objection_state, 1),
                (assign, "$npc_with_political_grievance", ":var_6"),
            (try_end),
        (else_try),
            (neg|main_party_has_troop, ":var_6"),
            (eq, ":var_7", 5),
            (troop_get_slot, ":var_13", ":var_6", slot_troop_current_mission),
            (try_begin),
                (gt, ":var_13", 0),
                (val_sub, ":var_13", 1),
                (troop_set_slot, ":var_6", slot_troop_current_mission, ":var_13"),
            (else_try),
                (troop_slot_ge, ":var_6", slot_troop_mission_object, 1),
                (this_or_next|neg|troop_slot_eq, ":var_6", slot_troop_mission_object, 8),
                (hero_can_join, "p_main_party"),
                (assign, "$npc_to_rejoin_party", ":var_6"),
            (try_end),
        (try_end),
    (try_end),
]),

(1.000000, 0.000000, 0.000000,
[
    (troop_get_type, ":var_0", "trp_player"),
    (ge, ":var_0", 1),
    (try_for_range, ":var_1", "trp_npc_adonja", "trp_kingdom_1_lord"),
        (troop_slot_eq, ":var_1", slot_troop_occupation, 5),
        (troop_get_inventory_capacity, ":var_2", ":var_1"),
        (try_for_range, ":var_3", 0, ":var_2"),
            (troop_get_inventory_slot, ":var_4", ":var_1", ":var_3"),
            (ge, ":var_4", 0),
            (this_or_next|eq, ":var_4", "itm_pop_ebony_great_sword"),
            (this_or_next|eq, ":var_4", "itm_pop_havathang"),
            (eq, ":var_4", "itm_strange_great_sword"),
            (unlock_achievement, 80),
            (assign, ":var_2", 0),
        (try_end),
    (try_end),
],
[
]),

(0.100000, 0.000000, ti_once,
[
],
[
    (call_script, "script_kt_init_item_slots"),
    (item_set_slot, "itm_no_item", 101, 3),
]),


]